<?php
namespace Common\Logic;

/**
 * 预定端支付逻辑
 * @modify trainyao 2016-05-16 创建
 */
class PayLogic extends \Common\Controller\BaseController {
    /**
     * 执行支付宝支付
     * 方法会抛异常
     * @modify trainyao 2016-05-16 创建
     * @param  array   $param     支付宝回调信息
     * @return boolean                执行结果
     */
    public function aliPay($param) {
        $orderNumber = isset($param['out_trade_no']) ? $param['out_trade_no'] : '';
        $sellerId = isset($param['seller_id']) ? $param['seller_id'] : '';
        
        if ($sellerId == '') {
            throw new \Exception('没有seller_id ' . $orderNumber, 1);
            return false;
        }
        if ($orderNumber == '') {
            throw new \Exception('没有订单号 ' . $orderNumber, 1);
            return false;
        }
        
        // 支付宝校验接口调用有效性
        vendor('Alipay.AlipayNotify');
        $alipayConfig = C('ALIPAY_CONFIG');
        $alipayNotify = new \AlipayNotify($alipayConfig);
        $verifyResult = $alipayNotify->verifyNotify();
        if ($verifyResult) {
            // 调用RPC接口进行支付
            Vendor('phpRPC.phprpc_client');
            $client = new \PHPRPC_Client(C('ORDER_RPC_URL'));
            $client->setEncryptMode(3);
            $RPCData = array(
                'flag' => C('MEIZHU_RPC_FLAG'),
                'time' => time(),
                'aliPayData' => $param,
                'orderNumber' => $orderNumber,
            );
            $result = $client->aliPay($RPCData);

            if ($result['httpstatus'] != 200) {
                throw new \Exception('美住管理端RPC调用失败:' . $result['httpstatus'] . ',' . $result['status'] . ',' . $result['info'], 1);
                return false;
            }
            return true;
        } else {
            // 验证失败,非法请求
            throw new \Exception('支付宝支付回调验证失败,可能是非法请求:' . $orderNumber, 1);
            return false;
        }
    }

    /**
     * 执行支付宝支付
     * 方法会抛异常
     * @modify wenhui 2018-01-18 创建
     * @param  array   $param     支付宝回调信息
     * @return boolean                执行结果
     */
    public function chargeAliPay($param) {
        $orderNumber = isset($param['out_trade_no']) ? $param['out_trade_no'] : '';
        $sellerId = isset($param['seller_id']) ? $param['seller_id'] : '';
        $vipUserId = isset($param['extra_common_param']) ? urldecode($param['extra_common_param']) : '';
        
        if ($sellerId == '') {
            throw new \Exception('没有seller_id ' . $orderNumber, 1);
            return false;
        }
        if ($orderNumber == '') {
            throw new \Exception('没有订单号 ' . $orderNumber, 1);
            return false;
        }
        
        // 支付宝校验接口调用有效性
        vendor('Alipay.AlipayNotify');
        $alipayConfig = C('ALIPAY_CONFIG');
        $alipayNotify = new \AlipayNotify($alipayConfig);
        $verifyResult = $alipayNotify->verifyNotify();
        if ($verifyResult) {
            // 调用RPC接口进行支付
            Vendor('phpRPC.phprpc_client');
            $client = new \PHPRPC_Client(C('ORDER_RPC_URL'));
            $client->setEncryptMode(3);
            $RPCData = array(
                'flag' => C('MEIZHU_RPC_FLAG'),
                'time' => time(),
                'aliPayData' => $param,
                'orderNumber' => $orderNumber,
                'vipUserId' => $vipUserId,
            );
            $result = $client->chargeAliPay($RPCData);
            if ($result['httpstatus'] != 200) {
                throw new \Exception('美住管理端RPC调用失败:' . $result['httpstatus'] . ',' . $result['status'] . ',' . $result['info'], 1);
                return false;
            }
            return true;
        } else {
            // 验证失败,非法请求
            throw new \Exception('支付宝支付回调验证失败,可能是非法请求:' . $orderNumber, 1);
            return false;
        }
    }

    function getPayType($hotelId, $payType){
        $hotelPayUsableModel = D('HotelPayUsable');
        $hotelPayUsableInfo = $hotelPayUsableModel->getInfo($hotelId);
        $payUsable = array(
            'uclbrt_alipay_enable'=>!empty($hotelPayUsableInfo['uclbrtalipayenable']) && time()<strtotime($hotelPayUsableInfo['uclbrtalipayenddate']),
            'uclbrt_wechat_enable'=>!empty($hotelPayUsableInfo['uclbrtwechatenable']) && time()<strtotime($hotelPayUsableInfo['uclbrtwechatenddate']),
            'hotel_alipay_enable'=>!empty($hotelPayUsableInfo['hotelalipayenable']) && time()<strtotime($hotelPayUsableInfo['hotelalipayenddate']),
            'hotel_wechat_enable'=>!empty($hotelPayUsableInfo['hotelwechatenable']) && time()<strtotime($hotelPayUsableInfo['hotelwechatenddata']),
        );
        $result = array(
            'uclbrt_alipay' => $payUsable['uclbrt_alipay_enable'] && ($payType & 1),
            'uclbrt_wechat' => $payUsable['uclbrt_wechat_enable'] && ($payType & 2),
            'hotel_alipay' => $payUsable['hotel_alipay_enable'] && ($payType & 16),
            'hotel_wechat' => $payUsable['hotel_wechat_enable'] && ($payType & 32),
        );
        $result['alipay'] = $result['uclbrt_alipay'] || $result['hotel_alipay'];
        $result['wechat'] = $result['uclbrt_wechat'] || $result['hotel_wechat'];
        $result['vipcardpay'] = ($payType & 64);
        return $result;
    }

    function getHotelPayType($hotelId, $payType){
        $hotelPayUsableModel = D('HotelPayUsable');
        $hotelPayUsableInfo = $hotelPayUsableModel->getInfo($hotelId);
        $payUsable = array(
            'hotel_alipay_enable'=>!empty($hotelPayUsableInfo['hotelalipayenable']) && time()<strtotime($hotelPayUsableInfo['hotelalipayenddate']),
            'hotel_wechat_enable'=>!empty($hotelPayUsableInfo['hotelwechatenable']) && time()<strtotime($hotelPayUsableInfo['hotelwechatenddata']),
        );
        $result = array(
            'hotel_alipay' => $payUsable['hotel_alipay_enable'] && ($payType & 16),
            'hotel_wechat' => $payUsable['hotel_wechat_enable'] && ($payType & 32),
        );

        return $result;
    }
}
